/*
 * Copyright 2010 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.mgwt.examples.showcase.client.activities.animation;

import java.util.List;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.dom.client.event.tap.HasTapHandlers;
import com.googlecode.mgwt.examples.showcase.client.AppController;
import com.googlecode.mgwt.examples.showcase.client.ChromeWorkaround;
import com.googlecode.mgwt.examples.showcase.client.ServiceCallRenderer;
import com.googlecode.mgwt.examples.showcase.shared.ServiceCallDTO;
import com.googlecode.mgwt.ui.client.MGWT;
import com.googlecode.mgwt.ui.client.MGWTStyle;
import com.googlecode.mgwt.ui.client.dialog.ConfirmDialog;
import com.googlecode.mgwt.ui.client.dialog.ConfirmDialog.ConfirmCallback;
import com.googlecode.mgwt.ui.client.dialog.Dialogs;
import com.googlecode.mgwt.ui.client.dialog.Dialogs.OptionCallback;
import com.googlecode.mgwt.ui.client.dialog.Dialogs.OptionsDialogEntry;
import com.googlecode.mgwt.ui.client.widget.Button;
import com.googlecode.mgwt.ui.client.widget.HeaderButton;
import com.googlecode.mgwt.ui.client.widget.HeaderPanel;
import com.googlecode.mgwt.ui.client.widget.LayoutPanel;
import com.googlecode.mgwt.ui.client.widget.ProgressIndicator;
import com.googlecode.mgwt.ui.client.widget.ScrollPanel;
import com.googlecode.mgwt.ui.client.widget.WidgetList;

/**
 * @author Daniel Kurka
 * 
 */
public abstract class AbstractServiceRequestView implements ServiceRequesView {

	private LayoutPanel main;
	private FlowPanel flowPanel;
	private HeaderPanel headerPanel;
	private HeaderButton headerBackButton;
	private Button postButton ;
//	private List<OptionsDialogEntry> confirmationButons;
	private ProgressIndicator progressIndicator;
//	MListBox buildingListBox = new MListBox();
	

	/**
	 * 
	 */
	public AbstractServiceRequestView() {
		ScrollPanel scrollPanel = new ScrollPanel();
		scrollPanel.setUsePos(MGWT.getOsDetection().isAndroid());
		scrollPanel.setScrollingEnabledX(false);
		ChromeWorkaround.maybeUpdateScroller(scrollPanel);
		postButton = new Button(AppController.getClientFactory().getUserProfile().getCurrentUser().getLabels().post());
		 progressIndicator = new ProgressIndicator();
		progressIndicator.getElement().setAttribute("style", "margin:auto; margin-top: 50px");
		progressIndicator.setVisible(false);
		main = new LayoutPanel();
		flowPanel = new FlowPanel();
	
		headerPanel = new HeaderPanel();
		headerBackButton = new HeaderButton();
		scrollPanel.setWidget(flowPanel);
		initHeader();
		initModel();
		initView();
		main.add(headerPanel);
		main.add(scrollPanel);

	}
	
	abstract protected void initModel();

	

//	private void initConfirmationButtons() {
//		confirmationButons = new ArrayList<OptionsDialogEntry>();
//		confirmationButons.add(new OptionsDialogEntry(AppController.getClientFactory().getLabelResource().ok(), ButtonType.CONFIRM));
//		confirmationButons.add(new OptionsDialogEntry(AppController.getClientFactory().getLabelResource().cancel(), ButtonType.NORMAL));
//		
//	}



	private void initHeader() {
		headerPanel.setLeftWidget(headerBackButton);
		headerBackButton.setBackButton(true);
		headerBackButton.setVisible(!MGWT.getOsDetection().isAndroid());
		HTML header2 = new HTML(AppController.getClientFactory().getUserProfile().getCurrentUser().getLabels().problemLocation());
		header2.addStyleName(MGWTStyle.getTheme().getMGWTClientBundle().getListCss().listHeader());
		flowPanel.add(header2);
		
	}

	private void initView() {
		WidgetList widgets = getForm();
		widgets.add(postButton);
		flowPanel.setStyleName("dir");
		flowPanel.add(progressIndicator);
		flowPanel.add(widgets);
		
	}


	@Override
	public Widget asWidget() {
		return main;
	}

	@Override
	public void setTitle(String text) {
		headerPanel.setCenter(text);

	}

	@Override
	public HasTapHandlers getBackButton() {
		return headerBackButton;
	}

	

	@Override
	public void setLeftButtonText(String text) {
		headerBackButton.setText(text);

	}



	@Override
	public HasTapHandlers getPostButton() {
		return postButton;
	}


	@Override
	public boolean validate() {
//		if(buildingListBox.getSelectedIndex() < 0 ||
//				addressListBox.getSelectedIndex() < 0 ||
//				floorListBox.getSelectedIndex() < 0 ){
//			Dialogs.alert(AppController.getClientFactory().getLabelResource().error(), 
//					AppController.getClientFactory().getLabelResource().emptyFields(), null);
//			return false;
//		}
		return true;
	}



	@Override
	public void confirm(List<OptionsDialogEntry> list, OptionCallback callback) {
		Dialogs.options(list, callback, main);
	}

	@Override
	public void confirm(String title, String msg,ConfirmCallback confirmCallback) {
		ConfirmDialog confirmDialog = new ConfirmDialog(MGWTStyle.getTheme().getMGWTClientBundle().getDialogCss(),
				title, msg, confirmCallback,AppController.getClientFactory().getUserProfile().getCurrentUser().getLabels().ok(),
				AppController.getClientFactory().getUserProfile().getCurrentUser().getLabels().cancel());
		confirmDialog.show();
		
	}


	@Override
	public void setBussy(boolean b) {
		progressIndicator.setVisible(b);
		postButton.setVisible( ! b);
	}

	@Override
	public void showDuplicateMsg(ServiceCallDTO dto) {
		ServiceCallRenderer renderer = AppController.getClientFactory().getUserProfile().getCurrentUser().getServiceCallRenderer();
		Dialogs.alert(renderer.getDisplayName(dto), 
				AppController.getClientFactory().getUserProfile().getCurrentUser().getLabels().duplicateReport()+
				DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT).format(dto.getDate()), null);
		
		
	}





}
